<?php

//Date:24-11-2012
//Booking class for unsing EAN API
//Author:Andrei-Petru Puf

class Book {

    private $urlParameters = array(
        0 => "CID",
        1 => "apiKey",
        2 => "customerIpAddress",
        3 => "customerSessionId",
        4 => "minorRev",
        5 => "locale",
        6 => "lang",
        7 => "currencyCode"
    );
    private $url;
    private $xml;
    private $patameters;
    private $bookData;

    public function Book($cid, $apiKey, $customerIpAddress, $customerSessionId, $minorRev, $locale, $lang, $currencyCode) {

        $this->url = "https://book.api.ean.com/ean-services/rs/hotel/v3/res?";

        $this->parameters[0] = $cid;
        $this->parameters[1] = $apiKey;
        $this->parameters[2] = $customerIpAddress;
        $this->parameters[3] = $customerSessionId;
        $this->parameters[4] = $minorRev;
        $this->parameters[5] = $locale;
        $this->parameters[6] = $lang;
        $this->parameters[7] = $currencyCode;
    }

    public function SetGeneralData(
    $hotelId, $arrivalDate, $departureDate, $supplierType, $rateKey, $roomTypeCode, $rateCode, $chargeableRate) {

        $this->bookData["hotelId"] = $hotelId;
        $this->bookData["arrivalDate"] = $arrivalDate;
        $this->bookData["departureDate"] = $departureDate;
        $this->bookData["supplierType"] = $supplierType;
        $this->bookData["rateKey"] = $rateKey;
        $this->bookData["roomTypeCode"] = $roomTypeCode;
        $this->bookData["rateCode"] = $rateCode;
        $this->bookData["chargeableRate"] = $chargeableRate;
    }

    public function SetGuestsData($numberOfRooms, $roomsData) {
        $this->bookData['numberOfRooms'] = $numberOfRooms;

        for ($i = 0; $i < $numberOfRooms; $i++) {
            $this->bookData["room"][$i]['numberOfAdults'] = $roomsData[$i]['numberOfAdults'];
            $this->bookData["room"][$i]['numberOfChildren'] = $roomsData[$i]['numberOfChildren'];
            $this->bookData["room"][$i]['childAges'] = $roomsData[$i]['childAges'];
            $this->bookData["room"][$i]['firstName'] = $roomsData[$i]['firstName'];
            $this->bookData["room"][$i]['lastName'] = $roomsData[$i]['lastName'];
            $this->bookData["room"][$i]['smokingPreference'] = $roomsData[$i]['smokingPreference'];
        }
    }

    public function SetReservationInfo($reservationInfo) {
        $this->bookData['reservationInfo']['email'] = $reservationInfo['email'];
        $this->bookData['reservationInfo']['firstName'] = $reservationInfo['firstName'];
        $this->bookData['reservationInfo']['lastName'] = $reservationInfo['lastName'];
        $this->bookData['reservationInfo']['homePhone'] = $reservationInfo['homePhone'];
        $this->bookData['reservationInfo']['workPhone'] = $reservationInfo['workPhone'];
        $this->bookData['reservationInfo']['creditCardType'] = $reservationInfo['creditCardType'];
        $this->bookData['reservationInfo']['creditCardNumber'] = $reservationInfo['creditCardNumber'];
        $this->bookData['reservationInfo']['creditCardExpirationMonth'] = $reservationInfo['creditCardExpirationMonth'];
        $this->bookData['reservationInfo']['creditCardExpirationYear'] = $reservationInfo['creditCardExpirationYear'];
        $this->bookData['reservationInfo']['creditCardIdentifier'] = $reservationInfo['creditCardIdentifier'];
    }

    public function SetAddressInfo($addressInfo) {
        $this->bookData['addressInfo']['address1'] = $addressInfo['address1'];
        $this->bookData['addressInfo']['city'] = $addressInfo['city'];
        $this->bookData['addressInfo']['stateProvinceCode'] = $addressInfo['stateProvinceCode'];
        $this->bookData['addressInfo']['countryCode'] = $addressInfo['countryCode'];
        $this->bookData['addressInfo']['postalCode'] = $addressInfo['postalCode'];
    }

    public function CreateXML_Request() {
        $this->xml = "";
        $this->xml.="<HotelRoomReservationRequest>";
        $this->xml.="<hotelId>" . $this->bookData["hotelId"] . "</hotelId>";
        $this->xml.="<arrivalDate>" . $this->bookData["arrivalDate"] . "</arrivalDate>";
        $this->xml.="<departureDate>" . $this->bookData["departureDate"] . "</departureDate>";
        $this->xml.="<supplierType>" . $this->bookData["supplierType"] . "</supplierType>";
        $this->xml.="<rateKey>" . $this->bookData["rateKey"] . "</rateKey>";
        $this->xml.="<rateCode>" . $this->bookData["rateCode"] . "</rateCode>";
        $this->xml.="<roomTypeCode>" . $this->bookData["roomTypeCode"] . "</roomTypeCode>";
        $this->xml.="<chargeableRate>" . $this->bookData["chargeableRate"] . "</chargeableRate>";
        $this->xml.="<RoomGroup>";
        for ($i = 0; $i < $this->bookData['numberOfRooms']; $i++) {
            $this->xml.="<Room>";
            $this->xml.="<numberOfAdults>" . $this->bookData["room"][$i]['numberOfAdults'] . "</numberOfAdults>";
            $this->xml.="<numberOfChildren>" . $this->bookData["room"][$i]['numberOfChildren'] . "</numberOfChildren>";
            $this->xml.="<childAges>" . $this->bookData["room"][$i]['childAges'] . "</childAges>";
            $this->xml.="<firstName>" . $this->bookData["room"][$i]['firstName'] . "</firstName>";
            $this->xml.="<lastName>" . $this->bookData["room"][$i]['lastName'] . "</lastName>";
            $this->xml.="<smokingPreference>" . $this->bookData["room"][$i]['smokingPreference'] . "</smokingPreference>";
            $this->xml.="</Room>";
        }
        $this->xml.="</RoomGroup>";
        $this->xml.="<ReservationInfo>";
        $this->xml.="<email>" . $this->bookData['reservationInfo']['email'] . "</email>";
        $this->xml.="<firstName>" . $this->bookData['reservationInfo']['firstName'] . "</firstName>";
        $this->xml.="<lastName>" . $this->bookData['reservationInfo']['lastName'] . "</lastName>";
        $this->xml.="<homePhone>" . $this->bookData['reservationInfo']['homePhone'] . "</homePhone>";
        $this->xml.="<workPhone>" . $this->bookData['reservationInfo']['workPhone'] . "</workPhone>";
        $this->xml.="<creditCardType>" . $this->bookData['reservationInfo']['creditCardType'] . "</creditCardType>";
        $this->xml.="<creditCardNumber>" . $this->bookData['reservationInfo']['creditCardNumber'] . "</creditCardNumber>";
        $this->xml.="<creditCardExpirationMonth>" . $this->bookData['reservationInfo']['creditCardExpirationMonth'] . "</creditCardExpirationMonth>";
        $this->xml.="<creditCardExpirationYear>" . $this->bookData['reservationInfo']['creditCardExpirationYear'] . "</creditCardExpirationYear>";
        $this->xml.="<creditCardIdentifier>" . $this->bookData['reservationInfo']['creditCardIdentifier'] . "</creditCardIdentifier>";
        $this->xml.="</ReservationInfo>";

        $this->xml.="<AddressInfo>";
        $this->xml.="<address1>" . $this->bookData['addressInfo']['address1'] . "</address1>";
        $this->xml.="<city>" . $this->bookData['addressInfo']['city'] . "</city>";
        $this->xml.="<stateProvinceCode>" . $this->bookData['addressInfo']['stateProvinceCode'] . "</stateProvinceCode>";
        $this->xml.="<countryCode>" . $this->bookData['addressInfo']['countryCode'] . "</countryCode>";
        $this->xml.="<postalCode>" . $this->bookData['addressInfo']['postalCode'] . "</postalCode>";
        $this->xml.="</AddressInfo>";
        $this->xml.="</HotelRoomReservationRequest>";


        //print_r($this->xml);
    }

    function SendRequest() {

        $this->url.=$this->urlParameters[0] . "=" . $this->parameters[0] . "&";
        $this->url.=$this->urlParameters[1] . "=" . $this->parameters[1] . "&";
        $this->url.=$this->urlParameters[2] . "=" . $this->parameters[2] . "&";
        $this->url.=$this->urlParameters[3] . "=" . $this->parameters[3] . "&";
        $this->url.=$this->urlParameters[4] . "=" . $this->parameters[4] . "&";
        $this->url.=$this->urlParameters[5] . "=" . $this->parameters[5] . "&";
        $this->url.=$this->urlParameters[6] . "=" . $this->parameters[6] . "&";
        $this->url.=$this->urlParameters[7] . "=" . $this->parameters[7] . "&";
        $this->url.="xml" . "=" . urlencode($this->xml);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 65000);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json", "Content-type: application/x-www-form-urlencoded", "Content-length: 0"));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
        $error = curl_error($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        return $response;
    }

}

?>